<?php
    /**
     * @name 视频类
     * @copyright tcsos inc
     * @author zhouqi 2012-1-4 PM 05:44:04
     * @version cooperation v1.0
     */
    class video {

        //视频允许解析站点
        private $video_site = 'youku.com|tudou.com|ku6.com|sina.com.cn|sohu.com|56.com';

        //视频URL
        private $url = '';

        //站点url
        private $host = '';

        /**
         * video_parse 视频解析
         *                   
         * @param array          
         * @return bool
         */
        public function _parse($url) {

            if(!$url) {
                return false;
            }                                    

            //是否属于合法解析视频站点
            if(!preg_match("/({$this->video_site})/i", $url, $this->host)) {
                return false;
            }                        

            $this->url = $url;
            $this->host = $this->host[0];

            //定义方法数组
            $fun_ary = array(
            'youku.com' => array('fun' => '_parse_yk', 'site' => '优酷视频'),        //优酷
            'tudou.com' => array('fun' => '_parse_tudou', 'site' => '土豆视频'),     //土豆
            'ku6.com' => array('fun' => '_parse_ku6', 'site' => '酷6视频'),         //酷6
            'sohu.com' => array('fun' => '_parse_sohu', 'site' => '搜狐视频'),       //搜狐
            'sina.com.cn' => array('fun' => '_parse_sina', 'site' => '新浪视频'),    //新浪
            '56.com' => array('fun' => '_parse_56', 'site' => '56视频'),           //56视频
            );

            //调用方法
            $result =  $this->$fun_ary[$this->host]['fun']();

            if(!$result) {
                return false;
            }

            $result['site'] = $fun_ary[$this->host]['site'];
            return $result;
        }

        /**
         * parse_yk 优酷视频解析
         * 1、http://v.youku.com/v_show/id_XNDg1MjA0ODg=.html    
         * 2、http://v.youku.com/v_playlist/f6298630o1p0.html   
         * 
         * @author zhouqi 2012-1-4 PM 05:55:57         
         * @return array
         */
        private function _parse_yk() {
            $flag = true;

            if(($flag = !strpos($this->url, 'v_show')) && !strpos($this->url, 'v_playlist')) {
                return false;
            }

            //获取优酷ID
            if(!$flag) {                    
                //模式1
                preg_match_all("/id\_(\w+)[=.]/", $this->url, $matches);
            } else {
                //模式2
                $content = http_curl($this->url);
                $matches[1][0] = $this->get_script_by_name('videoId', $content, ';');                        
            }

            if(!$matches[1][0]) {
                return false;
            }

            //根据ID获取优酷FLASH地址
            //http://v.youku.com/player/getPlayList/VideoIDS/视频的ID/version/5/source/out?onData=%5Btype%20Function%5D&n=3
            $data_row = http_curl("http://v.youku.com/player/getPlayList/VideoIDS/{$matches[1][0]}/version/5/source/out?onData=%5Btype%20Function%5D&n=3");

            if(!$data_row) {
                return false;
            }

            //数据解密
            $data_row = json_decode($data_row, true);

            //小图
            $img_str = (string)$data_row['data'][0]['logo'];
            $img_name = basename($img_str);
            $img_dir = dirname($img_str);

            $result['img'] = $img_dir . '/0' . substr($img_name, 1);
            $result['title'] = (string)$data_row['data'][0]['title'];
            $result['view'] = "http://player.youku.com/player.php/sid/{$matches[1][0]}/v.swf";
            $result['id'] = $matches[1][0];            

            return $result;
        }

        /**
         * 土豆视频解析
         * 1、http://www.tudou.com/listplay/TmbTY7d0FSI.html
         * 2、http://www.tudou.com/programs/view/mff0-M37B2Q/
         * 
         * @author zhouqi 2012-1-5 AM 10:38:52
         * @return array
         */
        private function _parse_tudou() {
            $flag = true;

            if(($flag = !strpos($this->url, 'listplay')) && !strpos($this->url, 'programs')) {
                return false;
            }

            $content = http_curl($this->url);

            preg_match("/pageId([\s\S]*?)\<\/script\>/i", $content, $content);
            $content = $content[1];

            if(!$content) {
                return false;
            }

            if(!$flag) {

                //获取临时id
                $tmp_id = str_replace('.html', '', basename($this->url));

                //获取jsonid
                $iid = $this->get_script_by_name('listData', $content);
                $iid = explode(':', $iid);
                $iid = $iid[1];                    

                //获取数据
                $result['img'] = $this->get_script_by_name('pic', $content);
                $result['title'] = iconv('gb2312', 'utf-8', $this->get_script_by_name('lkw', $content));
                $result['view'] = "http://www.tudou.com/l/{$tmp_id}/&resourceId=0_05_05_99&iid={$iid}&bid=05/v.swf";
                $result['id'] = $iid;                                                
                return $result;
            }

            //获取临时id
            $tmp_id = $this->get_script_by_name('icode', $content);

            //获取数据
            $result['img'] = $this->get_script_by_name('pic', $content);
            $result['title'] = iconv('gb2312', 'utf-8', $this->get_script_by_name('kw', $content));
            $result['view'] = "http://www.tudou.com/v/{$tmp_id}/&resourceId=0_04_05_99/v.swf";
            $result['id'] = $this->get_script_by_name('iid', $content);

            return $result;
        }

        /**
         * parse_kul 酷6视频解析
         * 1、http://v.ku6.com/show/Mtb8riNelR6NsXly.html        
         * 2、http://v.ku6.com/special/show_4232596/u4XWcTkQpkcx1-0_.html
         * 3、http://v.ku6.com/film/show_129810/cu8OLvMg8g-vznrY.html    
         *
         * @author zhouqi 2012-1-5 AM 10:38:58         
         * @return array
         */
        private function _parse_ku6() {
            //是否属于酷6结构
            if(!stripos($this->url, '/special/') && !($flag = stripos($this->url, '/film/')) && !stripos($this->url, '/show/')) {
                return false;
            }

            //获取内容
            $content = http_curl($this->url);

            preg_match("/commvid([\s\S]*?)\}/i", $content, $content);
            $content = $content[1];

            if(!$content) {
                return false;
            }

            //获取数据
            $result['id'] = $this->get_script_by_name('id', $content);         //获取ID
            $result['view'] = 'http://player.ku6.com/refer/' . $result['id'] . '/v.swf';      //获取视频
            $tmp_title = $this->get_script_by_name('title', $content);         //临时标题
            $tmp_title = json_decode('{"title":"' . $tmp_title . '"}', true);        //临时标题
            $result['title'] = $tmp_title['title'];       //标题
            $result['img'] = $this->get_script_by_name('cover', $content);        //图片

            return $result;
        }

        /**
         * parse_sohu 搜狐视频解析
         * http://tv.sohu.com/20110719/n313860515.shtml
         * 
         * @author zhouqi 2012-1-5 AM 10:42:02         
         * @return array
         */
        private function _parse_sohu() {

            $content = http_curl($this->url);

            preg_match("/\=\"JavaScript\"\>([\s\S]*?)\<\/script\>/i", $content, $content);
            $content = $content[1];

            if(!$content) {
                return;
            }

            $result['id'] = $this->get_script_by_name('vid', $content, ';');      //获取ID
            $result['img'] = $this->get_script_by_name('cover', $content, ';');       //图片
            $result['title'] = iconv('gb2312', 'utf-8', $this->get_script_by_name('news_keyword', $content, ';'));      //获取标题
            $result['view'] = 'http://share.vrs.sohu.com/' . $result['id'] . '/v.swf';      //视频
            return $result;
        }

        /**
         * parse_sina 新浪视频解析
         * http://video.sina.com.cn/v/b/54158715-1393760700.html         
         * 
         * @author zhouqi 2012-1-5 AM 10:43:05         
         * @return array
         */
        private function _parse_sina() {
            $content = http_curl($this->url);

            preg_match("/video : \{([\s\S]*?)\}/i", $content, $content);
            $content = $content[1];

            if(!$content) {
                return;
            }

            $result['img'] = $this->get_script_by_name('pic', $content);      //图片
            $result['title'] = $this->get_script_by_name('title', $content);      //标题
            $result['view'] = $this->get_script_by_name('swfOutsideUrl', $content);       //视频
            $result['id'] = $this->get_script_by_name('vid ', $content);       //获取ID
            return $result;
        }

        /**
         * parse_56 视频解析
         * http://www.56.com/w98/play_album-aid-9944146_vid-Njk3OTA1ODY.html
         * http://www.56.com/u72/v_Njk5NTA4MjE.html
         *
         * @author zhouqi 2012-1-5 AM 10:41:09         
         * @return array
         */
        private function _parse_56() {                                
            $content = http_curl($this->url);

            if(strexists($this->url, 'play_album')) {
                
                preg_match("/URLid\=([\w]+)/i", $content, $id);
                preg_match("/tit\=([^&]+)/i", $content, $title);
                preg_match("/vid:\"([\w]+)\"/i", $content, $vid);
                
                if(!$id[1] || !$title[1] || !$vid[1]) {
                    return false;
                }
                
                $result['id'] = $id[1];
                $result['img'] = "http://img.v139.56.com/images/17/17/soyomusici56olo56i56.com_{$id[1]}.jpg?j=" . rand(1000, 9999);
                $result['title'] = $title[1];
                $result['view'] = "http://player.56.com/v_{$vid[1]}.swf";
            } else {
                preg_match("/Subject\"\:\"([^\"]+)/i", $content, $title);
                preg_match("/img\"\:\"([^\"]+)/i", $content, $id);
                
                if(!$title[1]) {
                    return false;
                }
                
                $result['id'] = trim(basename($this->url), '.html');
                $result['title'] = unicode_decode($title[1]);
                $result['img'] = str_replace("\\", '', $id[1]);
                $result['view'] = "http://player.56.com/" . trim(basename($this->url), '.html') . ".swf";
            }                       

            return $result;
        }

        /**
         * 获取script的某个变量名的值，主要是配合视频抓取
         *
         * @param string $name 字段名称
         * @param string $content 页面内容
         * @param string $sign  结尾符号
         * @return string
         */    
        private function get_script_by_name($name = '', $content = '', $sign = ',') {            

            $var['begin'] = strpos($content, $name);

            $content = substr($content, $var['begin'] + strlen($name) + 2);

            $var['end'] = strpos($content, $sign);

            $content = substr($content, 0, $var['end']);

            return trim(str_replace(array('"', '\''), '', $content));
        }
    }
?>